package com.chtw.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.chtw.utils.CodeUtil;

@WebServlet("/GetCodeServlet.do")
public class GetCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		if (action.equals("getCode")) {
			//生成验证码，放入session
			CodeUtil codeUtil = new CodeUtil();
			String code = codeUtil.getCode();
			session.setAttribute("code", code);
			//生成验证码图片
			BufferedImage image = codeUtil.getImage(code);
			// 将图片写入响应流
			OutputStream os = response.getOutputStream();
			ImageIO.write(image, "jpeg", os);
			os.flush();
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
